/**
 * 
 */
package org.istt.dao.hibernate;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.istt.dao.CategoryTypeDao;
import org.istt.domain.AttachFile;
import org.istt.domain.CategoryType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * @author Huy
 * 
 */
@Repository("categoryTypeDao")
public class CategoryTypeDaoHibernate extends
		BaseDaoHibernate<CategoryType, Long> implements CategoryTypeDao {
	@Autowired
	public CategoryTypeDaoHibernate(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	public int countAttachfileByCateType(final Long categoryTypeId) {
		// TODO Auto-generated method stub
		return hibernateTemplate.execute(new HibernateCallback<Integer>() {

			@Override
			public Integer doInHibernate(Session session)
					throws HibernateException, SQLException {
				return ((Long) session.createCriteria(AttachFile.class)
										.add(Restrictions.eq("categoryType.id", categoryTypeId))
										.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			}
			
		});
	}
}
